package com.itheima.store.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.itheima.store.dao.BookBorrowItemsDao;
import com.itheima.store.domain.BookBorrow;
import com.itheima.store.domain.BookBorrowItems;
import com.itheima.store.domain.PageBean;
import com.itheima.store.domain.SystemSetting;
import com.itheima.store.utils.JDBCUtils;

public class BookBorrowItemsDaoImpl implements BookBorrowItemsDao {

	@Override
	public String addBookBorrow(BookBorrowItems bbi) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql ="insert into bookborrowitems values(?,?,?,?,?,?,?)";
		String upsql ="update book_borrowing set bstatus =? where user_id =? and bstatus=?";
		Object[] params = { bbi.getBookBorrowItems_id(),bbi.getCount(),bbi.getBookBorrowing_id(),bbi.getBook_id(),
				bbi.getCreateTime(),bbi.getCreateUserId(),bbi.getCreateUserName()};
		Object[] upparams = {4,bbi.getCreateUserId(),3};
		int flag = queryRunner.update(sql, params);
		int flags = queryRunner.update(upsql, upparams);
		if(flag == 1){
			return "1";
		}else{
			return "0";
		}
		
	}

	@Override
	public Integer findCountByUserId(String user_id) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select count(*) from bookborrowitems where  createUserId = ?";
		Long count = (Long) queryRunner.query(sql, new ScalarHandler(), user_id);
		return count.intValue();
	}

	@Override
	public List<BookBorrowItems> findPageByUserId(String user_id, int begin, Integer pageSize) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from bookborrowitems where createUserId = ?  order by createTime desc limit ?,?";
		List<BookBorrowItems> list = queryRunner.query(sql, new BeanListHandler<BookBorrowItems>(BookBorrowItems.class), user_id,begin,pageSize);
		return list;
	}

	@Override
	public List<BookBorrow> findByOid(String oids) throws SQLException {
		String[] ids = oids.split(",");
		String  bookBorrowing_id = "";
		for(int i=0;i<ids.length;i++){
			bookBorrowing_id = bookBorrowing_id+"'"+ids[i]+"',";
		}
		if(bookBorrowing_id.endsWith(",")){
			bookBorrowing_id = bookBorrowing_id.substring(0, bookBorrowing_id.length()-1);
		}
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from book_borrowing where bookBorrowing_id in (?)  order by applicationTime desc";
		List<BookBorrow> list = queryRunner.query(sql, new BeanListHandler<BookBorrow>(BookBorrow.class), bookBorrowing_id);
		return list;
	}

	@Override
	public List<SystemSetting> findSetting() throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from sys_setting ";
		List<SystemSetting> list = queryRunner.query(sql, new BeanListHandler<SystemSetting>(SystemSetting.class));
		return list;
	}



	

	
	

}
